import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/meal_record_list.dart';
import 'package:ls_work/net/response/response_data.dart';
import 'package:ls_work/ui/meal/add_meal_page.dart';

class MealRecorderViewModel extends BaseListViewModel<MealRecordList>{
  final ElderInfo? info;
  MealRecorderViewModel(BuildContext context, {this.info}) : super(context){
    if(info!=null){
      elderNameLike = info?.name??"";
    }
  }

  var elderNameLike = "";
  // var createdDate = "";

  @override
  Future<List<MealRecordList>> requestListData() async {
    var map = await PostRequest.getMealRecord(elderNameLike, startDateStr);
    var bean = ResponseData<MealRecordList>.fromJson(map,listBuilder: (i)=>MealRecordList.fromJson(i));
    if("0"==bean.code){
      return bean.data?.rows??[];
    }else{
      throw(bean.msg??"");
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  void toAddSign() {
    Go().push(AddMealPage(info: info,));
  }

  @override
  void receiveMessage(SendMessageEvent event) {
    super.receiveMessage(event);
    if(event.msgCode==MessageConfig.FINISH_MEAL_SIGN){
      requestRefresh();
    }
  }

}
